package com.java.advance.custom.mybatis;

import com.java.advance.custom.mybatis.mapper.UserMapper;
import com.java.advance.custom.mybatis.pojo.User;
import com.java.advance.custom.mybatis.session.SqlSession;
import com.java.advance.custom.mybatis.session.SqlSessionFactory;

import java.util.List;

/**
 * Created by aivin on 2019/3/10.
 */
public class TestMybatis {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //1、实例化SqlSessionFactory，加载数据库配置信息
        SqlSessionFactory factory = new SqlSessionFactory();
        //2、获取sqlSession对象
        SqlSession sqlSession = factory.openSession();
        System.out.println(sqlSession);
        //3、通过动态代理跨越面向接口编程和ibatis编程模型的鸿沟
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //4、遵循jdbc规范，通过底层的四大对象的合作完成数据查询和数据转化
        User user = userMapper.selectByPrimaryKey(1);
        System.out.println(user);

        System.out.println("-------------------------------------");
        List<User> userList = userMapper.selectAll();
        for (User u : userList) {
            System.out.println(u);
        }
    }
}
